工控网首页
>

应用设计

>

PC与松下FP系列PLC的通信

PC与松下FP系列PLC的通信

2005/7/11 17:19:00
摘要:介绍了VB和松下FP 系列PLC 之间的通信协议,并给予了实例说明. 关键词:VB, FP. 一. 引言 PLC作为工业现场的控制器,以其性能稳定,编程简单,结构紧凑,通用性强等优点,已得到了广泛的运用;而计算器在图象处理,报表打印,中文显示等方面具有很强的功能;在现代的控制系统中,已经在很多地方将两者结合起来使用,充分发挥两者的优势,因此,PC和PLC之间的通信显的尤为重要,大多数PLC的通信协议都采用的是MODBUS,而松下FP系列采用的是其专用的MEWTOCOL-COM协议.本文通过实例详细讲解了利用该协议,达到PC和PLC通信的目的. 二. 松下PLC的通信协议 松下FP系列PLC的通信协议是采用松下公司专用的MEWTOCOL-COM协议.在该协议中,数据传送采用的是ASCII码形式,首先由计算器发送指令,PLC接受到指令后,自动进行相应的响应,计算器根据PLC的响应,可以得知PLC是否正确执行了刚刚发送的指令,这样就构成了一个交互式的操作,以确保PC 与 PLC之间的通信正常. MEWTOCOL-COM的指令格式如下: 指令信息:
应答信息(正常时):
应答信息(发生错误时):
其中, “ % ”为起始符号,标记每一帧报文的开始. “ CR ”为结束符号,标记每一帧报文的结束. “ AD ”为每一站PLC的地址,用两位十六进制数表示,如 01 则代表第一台PLC. “ # ”, “ $ ”, “ ! ”标记该帧报文为何种类型,其分别对应为:指令信息,应答信息(正常),应答信息(错误). “ BCC ”为校验码,为两位十六进制数,其初值为“ 0 ”,然后从起始符开始与该帧报文中每一字节按位进行异或运算得到的. 三. PC端VB程序的编写 PC端的程序我们采用VB6.0 和 MSCOMM32 控件来编写.VB的通信控件MSCOMM32提供了简单的串行口通信功能,使用它可以建立与串行口的连接,通过串行口连接到其它设备,发送命令,接受资料,以及监视和响应串行通信中发生的事件和错误等.我们只要通过上位机的程序,向PLC按照MEWTOCOL-COM协议规定的格式发送命令,PLC就可以作出相应的响应.为了使读者能够清楚的了解两者之间的通信,现举例如下: 整个程序的界面如图 (1) 所示.
程序的部分代码如下: Private Sub Cmd_Connect_Click() Lbl_State.Caption = "正在建立连接,请稍后..." DoEvents MSComm1.PortOpen = True ‘建立与串口的连接. MSComm1.InputLen = 1 MSComm1.RThreshold = 1 ‘设置接受缓冲区接受到一个字符就产生OnComm事件. TempOutputStr = "%" & Trim(Combo2.Text) & "#RT" ‘Combo2为PLC的站别. CommandStr = "RT" ‘ “RT”命令用来读取PLC当前的相关信息. MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) ‘将传送的内容显示在文本框中 Timer1.Enabled = True ‘ 系统开始计时,以判断PC和PLC连接是否正常. End If End Sub Private Sub Cmd_Dt_Click() Dim I As Integer Dim TempWd As String If Option3.Value = True Then CommandStr = "RD" For I = 0 To 15 Text1(I).Text = "" Next I TempOutputStr = "%" & Trim(Combo2.Text) & "#RD" & "D" & Trim(UCase(Txt_Bof.Text)) & Trim(UCase(Txt_Eof.Text)) ‘” RD “’命令用来读取DT中的数据. MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) Else CommandStr = "WD" TempOutputStr = "%" & Trim(Combo2.Text) & "#WD" & "D" & Trim(UCase(Txt_Bof.Text)) & Trim(UCase(Txt_Eof.Text)) ‘” WD “命令用来将数据写入到相应的DT中. For I = 0 To Val(Txt_Eof.Text) - Val(Txt_Bof.Text) TempWd = Hex$(Val(Text1(I).Text)) Do While Len(TempWd) <> 4 TempWd = "0" & TempWd Loop TempWd = Right$(TempWd, 2) & Left$(TempWd, 2) TempOutputStr = TempOutputStr & TempWd Next I MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) End If End Sub Private Sub Cmd_Run_Click() ‘控制PLC运行 CommandStr = "RM" RPBoolean = True MSComm1.Output = "%" & Trim(Combo2.Text) & "#RMR4A" & Chr$(13) Txt_PcPlc.Text = "%" & Trim(Combo2.Text) & "#RMR4A" End Sub Private Sub Cmd_Stop_Click() ‘控制PLC停止 CommandStr = "RM" RPBoolean = False MSComm1.Output = "%" & Trim(Combo2.Text) & "#RMP48" & Chr$(13) Txt_PcPlc.Text = "%" & Trim(Combo2.Text) & "#RMP48" End Sub Private Sub Cmd_TC_Click() Dim TempWs As String Dim TempWk As String If Option9.Value = True And Option11.Value = True Then CommandStr = "RS" ‘读取定时器/计数器中的默认值 TempOutputStr = "%" & Trim(Combo2.Text) & "#RS" & Trim(UCase(Txt_TC.Text)) & Trim(UCase(Txt_TC.Text)) MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) End If If Option9.Value = True And Option12.Value = True Then CommandStr = "WS" ‘写入定时器/计数器中的默认值 TempOutputStr = "%" & Trim(Combo2.Text) & "#WS" & Trim(UCase(Txt_TC.Text)) & Trim(UCase(Txt_TC.Text)) TempWs = Hex$(Val(Text2.Text)) Do While Len(TempWs) <> 4 TempWs = "0" & TempWs Loop TempWs = Right$(TempWs, 2) & Left$(TempWs, 2) TempOutputStr = TempOutputStr & TempWs MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) End If If Option10.Value = True And Option11.Value = True Then CommandStr = "RK" ‘读取定时器/计数器中的经过值 TempOutputStr = "%" & Trim(Combo2.Text) & "#RK" & Trim(UCase(Txt_TC.Text)) & Trim(UCase(Txt_TC.Text)) MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) End If If Option10.Value = True And Option12.Value = True Then CommandStr = "WK" ‘写入定时器/计数器中的经过值 TempOutputStr = "%" & Trim(Combo2.Text) & "#WK" & Trim(UCase(Txt_TC.Text)) & Trim(UCase(Txt_TC.Text)) TempWk = Hex$(Val(Text2.Text)) Do While Len(TempWk) <> 4 TempWk = "0" & TempWk Loop TempWk = Right$(TempWk, 2) & Left$(TempWk, 2) MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) End If End Sub Private Sub Cmd_YR_Click() Dim TempYR As String Dim TempValue As String Dim BinValue As String Dim HexValue As String Dim I As Integer If Option1.Value = True Then TempYR = "Y" Else TempYR = "R" End If If Option5.Value = True And Option7.Value = True Then CommandStr = "RCS" ‘读取单个触点的状态 TempOutputStr = "%" & Trim(Combo2.Text) & "#RCS" & TempYR & Trim(UCase(Txt_YR.Text)) MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) End If If Option6.Value = True And Option7.Value = True Then CommandStr = "WCS" ‘写入单个触点的状态 If Label22.BackColor = &HC0& Then TempValue = "1" Else TempValue = "0" End If TempOutputStr = "%" & Trim(Combo2.Text) & "#WCS" & TempYR & Trim(UCase(Txt_YR.Text)) & TempValue MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) End If If Option5.Value = True And Option8.Value = True Then CommandStr = "RCC" ‘读取字单元触点的状态 TempOutputStr = "%" & Trim(Combo2.Text) & "#RCC" & TempYR & Trim(UCase(Txt_YR.Text)) & Trim(UCa
投诉建议

提交

查看更多评论